
/* SOURCE-CODE-START */

"use strict";

var uiUtils = (function() {
if (typeof uiUtils !== 'undefined') {
  return uiUtils;
}



if (typeof $ === 'undefined') {
  throw new Error('jquery.js required');
} else if (typeof $.fn.bootstrapTable === 'undefined') {
  throw new Error('bootstrap-table.js required');
} else if (typeof $.fn.daterangepicker === 'undefined') {
  throw new Error('daterangepicker.js required');
} else if (typeof $.fn.jstree === 'undefined') {
  throw new Error('jstree.js required');
} else if (typeof globalUtils === 'undefined') {
  throw new Error('global-utils.js required');
} else if (typeof Pure === 'undefined') {
  throw new Error('pure-frontend.js required');
}




/**
 * @namespace uiUtils
 */

var uiUtils = {};




/**
 * @namespace uiUtils-common
 */

var _common = {};
uiUtils.common = _common;

/**
 * @memberof uiUtils-common
 * @description 检查 WebResult 数据结构的返回结果
 * @param {*} result 
 * @returns {boolean} 若 result.scuccess=true 则返回 result，否则弹出消息提示错误。
 */
_common.checkWebResultSuccess = function (result) {
  var success = (result.success === true);

  if (!success) {
    Pure.app.showError(result.message);
  }

  return success;
};

/**
 * @memberof uiUtils-common
 * @description 重置校验状态
 * @param {jQuery} jqElement 
 */
_common.resetValidation = function (jqElement) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  jqElement.validate()
    .resetForm();
};




/**
 * @namespace uiUtils-event
 */

var _event = {
  // 点击事件
  CLICK: 'click',
  // 模态框显示事件
  MODAL_SHOW: 'show.bs.modal',
  // 模态框隐藏事件
  MODAL_HIDE: 'hide.bs.modal',
  MODAL_HIDDEN: 'hidden.bs.modal',
  // 树形控件准备完成事件
  TREE_READY: 'ready.jstree',
  // 树形节点点击事件
  TREE_NODE_ACTIVATE: 'activate_node.jstree',
  // 表格加载成功事件
  DATA_GRID_LOAD_SUCCESS: 'load-success.bs.table'
};

uiUtils.event = _event;




/**
 * @namespace uiUtils-form
 */

var _form = {};
uiUtils.form = _form;

_form.uiData = {};
uiData.Model.config.initDataHandlers.autoTrimText = _form.uiData;

/**
 * @description 获取文本
 * @param {Element[]} elements DOM元素
 * @param {boolean} skipNull 是否跳过 null 值
 * @returns {*}
 */
_form.uiData.getValue = function (elements, skipNull) {
  var result = uiData.dataHandlers.defaultDataHandler.getValue(elements, skipNull);

  if (globalUtils.isNotEmptyString(result)) {
    result = globalUtils.trimString(result);
  }

  return result;
};

/**
 * @description 设置文本
 * @param {Element[]} elements DOM元素
 * @param {*} value 值
 * @param {boolean} defaultNull 是否默认 null 值
 */
_form.uiData.setValue = function (elements, value, defaultNull) {
  uiData.dataHandlers.defaultDataHandler.setValue(elements, value, defaultNull);
};




var _dataGrid = {};
uiUtils.dataGrid = _dataGrid;

/**
 * @namespace uiUtils-dataGrid
 */

/**
 * @memberof uiUtils-dataGrid
 * @description 初始表格
 * @param {jQuery} jqElement 
 * @param {*} [opts] - 方法 $().bootstrapTable([arg]) 的参数
 * @param {*} [opts.customQuery] - 允许添加额外的查询参数
 * @param {boolean} [opts.convertEmptyStringToNull] - 把 opts.customQuery 中的空字符串转换成 null 值, 值默认是 true 转换
 * @returns {jQuery}
 */
_dataGrid.init = function (jqElement, opts) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  opts = globalUtils.emptyObjectIfNullOrUndefined(opts);
  var customQuery = globalUtils.emptyObjectIfNullOrUndefined(opts.customQuery);

  if (!(opts.convertEmptyStringToNull === false)) {
    // 空字符串转换成 null 值
    customQuery = globalUtils.convertEmptyStringToNull(customQuery);
  }

  var initOpts = {
    // 默认请求方式是 POST 方式
    method: 'post',
    queryParamsType: '',
    // 默认分页
    pagination: true,
    // 默认每页最多 10 条
    pageSize: 10,
    // 默认服务端分页
    sidePagination: 'server',
    ajaxOptions: {

    },
    queryParams: function (params) {
      var newParams = globalUtils.mergeObjects(customQuery, params);

      return newParams;
    },
    responseHandler: function (res) {
      // 适配后端返回的数据格式
      return {
        total: res.total,
        rows: res.data
      };
    }
  };

  var newOpts = globalUtils.mergeObjects(initOpts, opts);
  jqElement.bootstrapTable(newOpts);

  return jqElement;
};

/**
 * @memberof uiUtils-dataGrid
 * @description 刷新表格
 * @param {jQuery} jqElement 
 * @returns {jQuery}
 */
_dataGrid.refresh = function (jqElement) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }


  jqElement.bootstrapTable('refresh');
  return jqElement;
};

/**
 * @memberof uiUtils-dataGrid
 * @description 新的查询
 * @param {jQuery} jqElement 
 * @param {*} [opts] - 方法 $().bootstrapTable('refreshOptions', [arg]) 的参数
 * @param {*} [opts.customQuery] - 允许添加额外的查询参数
 * @param {boolean} [opts.convertEmptyStringToNull] - 把 opts.customQuery 中的空字符串转换成 null 值, 值默认是 true 转换
 * @returns {jQuery}
 */
_dataGrid.newQuery = function (jqElement, opts) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  opts = globalUtils.emptyObjectIfNullOrUndefined(opts);
  var customQuery = globalUtils.emptyObjectIfNullOrUndefined(opts.customQuery);

  if (!(opts.convertEmptyStringToNull === false)) {
    // 空字符串转换成 null 值
    customQuery = globalUtils.convertEmptyStringToNull(customQuery);
  }

  var initOpts = {
    // 重置到第一页
    pageNumber: 1,
    queryParams: function (params) {
      var newParams = globalUtils.mergeObjects(customQuery, params);

      return newParams;
    }
  };

  var newOpts = globalUtils.mergeObjects(initOpts, opts);
  jqElement.bootstrapTable('refreshOptions', newOpts);

  return jqElement;
};

/**
 * @memberof uiUtils-dataGrid
 * @description 返回选中记录的 ID 数组
 * @param {jQuery} jqElement 
 * @param {string} [idPropName] 
 * @returns {any[]}
 */
_dataGrid.getIdSelections = function (jqElement, idPropName) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  if (globalUtils.isNullOrUndefined(idPropName)) {
    throw new Error('argument#1 "idPropName" is null/undefined');
  }

  var rows = jqElement.bootstrapTable('getSelections');
  if (!(rows instanceof Array)) {
    return [];
  }

  var ids = rows.map(function (row) {
    return row[idPropName];
  });

  return ids;
};

/**
 * @memberof uiUtils-dataGrid
 * @description 返回记录的字段数据
 * @param {jQuery} jqElement 
 * @param {string} [fieldName] 
 * @returns {any[]}
 */
_dataGrid.getFieldData = function (jqElement, fieldName) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  if (globalUtils.isNullOrUndefined(fieldName)) {
    throw new Error('argument#1 "fieldName" is null/undefined');
  }

  var rows = jqElement.bootstrapTable('getData');
  var values = rows.map(function (row) {
    return row[fieldName];
  });

  return values;
};

/**
 * @memberof uiUtils-dataGrid
 * @description 绑定复选框改变时的回调函数
 * @param {jQuery} jqElement 
 * @param {function} [callbackFn] 
 */
_dataGrid.onCheckBoxChange = function (jqElement, callbackFn) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  if (globalUtils.isNullOrUndefined(callbackFn)) {
    throw new Error('argument#1 "callbackFn" is null/undefined');
  }

  jqElement.on('check.bs.table uncheck.bs.table ' +
    'check-all.bs.table uncheck-all.bs.table ' +
    'refresh.bs.table refresh-options.bs.table ' +
    'load-success.bs.table page-change.bs.table', function () {
      var rows = jqElement.bootstrapTable('getSelections');
      callbackFn(rows);
    });
};


_dataGrid.uiData = {};
uiData.Model.config.initDataHandlers.dataGrid = _dataGrid.uiData;

/**
 * @description 获取表格数据
 * @param {Element[]} elements DOM元素
 * @param {boolean} skipNull 是否跳过 null 值
 * @returns {*}
 */
_dataGrid.uiData.getValue = function (elements, skipNull) {
  var jqElement = jQuery(elements[0]);
  var data = jqElement.bootstrapTable('getData');

  // 若未初始化则获取到的数据不是数组
  if (!(data instanceof Array)) {
    return [];
  }

  return _.cloneDeep(data);
};

/**
 * @description 设置表格数据
 * @param {Element[]} elements DOM元素
 * @param {*} value 值
 * @param {boolean} defaultNull 是否默认 null 值
 */
_dataGrid.uiData.setValue = function (elements, value, defaultNull) {
  var jqElement = jQuery(elements[0]);

  jqElement.bootstrapTable('load', (value || []));
};




/**
 * @namespace uiUtils-datePicker
 */

var _datePicker = {};
uiUtils.datePicker = _datePicker;
var DATA_TYPE_DATE_TEXT = 'dateText';

/**
 * @memberof uiUtils-datePicker
 * @description 初始日期时间
 * @param {jQuery} jqElement 
 * @param {*} [opts] - 方法 $().daterangepicker([arg]) 的参数
 * @returns {jQuery}
 */
_datePicker.init = function (jqElement, opts) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  opts = globalUtils.emptyObjectIfNullOrUndefined(opts);
  var initOpts = {
    // 仅显示开始日期
    singleDatePicker: true,
    // 不自动更新输入框
    autoUpdateInput: false
  };

  var dataType = jqElement.attr(globalConfig.dataTypeAttributeName);
  if (dataType === 'dateText') {
    initOpts.locale = $['daterangepicker-date-locale'];
  } else {
    initOpts.locale = $['daterangepicker-dateTime-locale'];
    initOpts.timePicker = true;
    initOpts.timePicker24Hour = true;
  }

  var newOpts = globalUtils.mergeObjects(initOpts, opts);
  jqElement.daterangepicker(newOpts);

  // 点击日期弹出框"确定"后更新输入框的值
  jqElement.on('apply.daterangepicker', function (ev, picker) {
    var dateText = picker.startDate.format(initOpts.locale.format);
    jqElement.val(dateText);
  });

  jqElement.on('cancel.daterangepicker', function (ev, picker) {
    jqElement.val('');
  });

  return jqElement;
};

_datePicker.uiData = {};
uiData.Model.config.initDataHandlers.dateText = _datePicker.uiData;
uiData.Model.config.initDataHandlers.dateTimeText = _datePicker.uiData;

/**
 * @description 获取日期
 * @param {Element[]} elements DOM元素
 * @param {boolean} skipNull 是否跳过 null 值
 * @returns {*}
 */
_datePicker.uiData.getValue = function (elements, skipNull) {
  var jqElement = jQuery(elements[0]);
  var datePicker = jqElement.data('daterangepicker');
  var momentDate = datePicker.startDate;
  var date = momentDate.toDate();

  if (globalUtils.isNullOrUndefined(date) || !globalUtils.isNotEmptyString(jqElement.val())) {
    return null;
  }

  var dataType = jqElement.attr(globalConfig.dataTypeAttributeName);
  var result;

  if (dataType === DATA_TYPE_DATE_TEXT) {
    result = globalUtils.formatDateWithZone(date);
  } else {
    result = globalUtils.formatDateTimeWithZone(date);
  }

  return result;
};

/**
 * @description 设置日期
 * @param {Element[]} elements DOM元素
 * @param {*} value 值
 * @param {boolean} defaultNull 是否默认 null 值
 */
_datePicker.uiData.setValue = function (elements, value, defaultNull) {
  var jqElement = jQuery(elements[0]);

  if (globalUtils.isNullOrUndefined(value)) {
    jqElement.val('');
  } else {
    var dataType = jqElement.attr(globalConfig.dataTypeAttributeName);
    var date;

    // 获取日期对象
    if (globalUtils.isNullOrUndefined(value) || (value instanceof Date)) {
      date = value;
    } else if (dataType === DATA_TYPE_DATE_TEXT) {
      date = globalUtils.parseDateWithZone(value);
    } else {
      date = globalUtils.parseDateTimeWithZone(value);
    }

    var datePicker = jqElement.data('daterangepicker');
    var dateFormat;

    // 获取日期格式
    if (dataType === DATA_TYPE_DATE_TEXT) {
      dateFormat = $['daterangepicker-date-locale'].format;
    } else {
      dateFormat = $['daterangepicker-dateTime-locale'].format;
    }

    var dateText = moment(date).format(dateFormat);
    // 更新输入框的值
    jqElement.val(dateText);
    // 更新日期选择器控件的开始日期和结束日期
    datePicker.setStartDate(date);
    datePicker.setEndDate(date);
  }
};





/**
 * @namespace uiUtils-tree
 */

var _tree = {};
uiUtils.tree = _tree;

/**
 * @memberof uiUtils-tree
 * @description 初始树形控件
 * @param {jQuery} jqElement 
 * @param {*} [opts] - 方法 $().jstree([arg]) 的参数
 * @returns {jQuery}
 */
_tree.init = function (jqElement, opts) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  opts = globalUtils.emptyObjectIfNullOrUndefined(opts);
  var initOpts = {
    'core': {
      // 默认最多选中一行
      'multiple': false,
      'themes': {
        // 默认不显示图标
        'icons': false
      }
    },
    'plugins': []
  };

  // 绑定事件
  jqElement.trigger('on_init.tree.uiUtils');

  var newOpts = globalUtils.mergeObjects(initOpts, opts);
  jqElement.jstree(newOpts);

  return jqElement;
};

/**
 * @memberof uiUtils-tree
 * @description 在初始时绑定事件
 * @param {jQuery} jqElement 
 * @param {...any} [dynamicArgs] 
 */
_tree.bindEventOnInit = function (jqElement) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  var dynamicArgs = [];

  for (var index = 1; index < arguments.length; ++index) {
    dynamicArgs.push(arguments[index]);
  }

  jqElement.on('on_init.tree.uiUtils', function () {
    jqElement.on.apply(jqElement, dynamicArgs);
  });
}

/**
 * @memberof uiUtils-tree
 * @description 销毁树形控件
 * @param {jQuery} jqElement 
 * @returns {jQuery}
 */
_tree.destroy = function (jqElement) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  jqElement.jstree('destroy');

  return jqElement;
};

/**
 * @memberof uiUtils-tree
 * @description 清空树形控件
 * @param {jQuery} jqElement 
 * @returns {jQuery}
 */
_tree.empty = function (jqElement) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  jqElement.jstree('destroy');

  return jqElement;
};

/**
 * @memberof uiUtils-tree
 * @description 返回勾选的节点数组
 * @param {jQuery} jqElement 
 * @returns {Array}
 */
_tree.getCheckedNodes = function (jqElement) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  return jqElement.jstree('get_checked', true);
};


/**
 * @memberof uiUtils-tree
 * @description 返回勾选节点的ID数组
 * @param {jQuery} jqElement 
 * @returns {Array}
 */
_tree.getCheckedIds = function (jqElement) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  return jqElement.jstree('get_checked');
};

/**
 * @memberof uiUtils-tree
 * @description 返回选中的节点数组
 * @param {jQuery} jqElement 
 * @returns {Array}
 */
_tree.getSelectedNodes = function (jqElement) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  return jqElement.jstree('get_selected', true);
};

/**
 * @memberof uiUtils-tree
 * @description 返回选中节点的ID数组
 * @param {jQuery} jqElement 
 * @returns {Array}
 */
_tree.getSelectedIds = function (jqElement) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }


  return jqElement.jstree('get_selected');
};





/**
 * @namespace uiUtils-dialog
 */

var DIALOG_HTML = [
  '<div class="modal" tabindex="-1">',
  '<div class="modal-dialog">',
  '<div class="modal-content">',
  '<div class="modal-header">',
  '<h5 class="modal-title">{0}</h5>',
  '<button type="button" class="close" data-dismiss="modal" aria-label="Close">',
  '<span aria-hidden="true">&times;</span>',
  '</button>',
  '</div>',
  '<div class="modal-body">',
  '<p>{1}</p>',
  '</div>',
  '<div class="modal-footer">',
  '<button type="button" class="btn btn-secondary x-btn-cancel" data-dismiss="modal">{2}</button>',
  '<button type="button" class="btn btn-primary x-btn-confirm">{3}</button>',
  '</div>',
  '</div>',
  '</div>',
  '</div>',
].join('\n');

var _dialog = {};
uiUtils.dialog = _dialog;

_dialog.BTN_CONFIRM = '.x-btn-confirm';
_dialog.BTN_CANCEL = '.x-btn-cancel';
var JQUERY_DATA_CONTEXT_DATA = 'uiUtils.dialog.contextData';
var JQUERY_DATA_CONTENT_BACKUP = 'uiUtils.dialog.contentBackup';

var CUSTOM_ATTR_INITED_FLAG = 'data-ui-utils-inited';
var CUSTOM_EVENT_ON_SHOW = 'on_show.dialog.uiUtils';
var CUSTOM_EVENT_ON_HIDE = 'on_hide.bs.modal.uiUtils';

/**
 * @memberof uiUtils-dialog
 * @description 显示确认框
 * @param {string} message 
 * @param {function} [confirmFn] 
 * @returns {jQuery}
 */
_dialog.showConfirmBox = function (message, confirmFn) {
  var dialogTitle = '${t("ui.system.common.dialog.title.operationTip")}';
  var confirmLabel = '${t("ui.system.common.dialog.btn.confirmLabel")}';
  var cancelLabel = '${t("ui.system.common.dialog.btn.cancelLabel")}';
  var dialogMessage = globalUtils.escapeHtml(message);

  var html = globalUtils.formatString(DIALOG_HTML, [dialogTitle, dialogMessage, cancelLabel, confirmLabel]);
  var jqElement = jQuery(html).prependTo('body');

  // 显示确认框
  jqElement.modal({
    keyboard: false,
    backdrop: 'static'
  });

  // 点击确认按钮的事件
  jqElement.on('click', _dialog.BTN_CONFIRM, function (event) {
    jqElement.modal('hide');

    if (globalUtils.isFunction(confirmFn)) {
      confirmFn();
    }
  });

  // 确认框关闭时移除 DOM 元素
  jqElement.on('hidden.bs.modal', function (event) {
    jqElement.remove();
  });

  return jqElement;
};

/**
 * @memberof uiUtils-dialog
 * @description 显示模态框
 * @param {jQuery} jqElement 
 * @param {View} [view]
 * @param {*} [opts] - 方法 $().modal([arg]) 的参数
 * @param {*} [opts.contextData] - 设置上下文数据, 在关闭对话框后会自动清除
 * @param {boolean} [opts.resetContent] - 使用 $().html 方法重置对话框内容, 值默认是 false 不重置
 * @param {boolean} [opts.resetData] - 使用 uiData.model([arg]).setData 方法重置对话框内容的数据, 值默认是 true 重置
 * @param {function} [opts.onModalShow] - 对话框显示时自动调用 onModalShow 方法
 * @param {function} [opts.onModalHide] - 对话框隐藏时自动调用 onModalHide 方法
 * @param {boolean} [opts.lockViewConfirmLeave] - 锁定视图在离开时需要确认, 值默认是 false 不锁定
 * @param {Element} [opts.backdropParent] - 自定义对话框背景的父标签
 * @returns {jQuery}
 */
_dialog.showModal = function (jqElement) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  if (jqElement.length <= 0) {
    throw new Error('argument#0 "jqElement" is empty');
  }

  var view;
  var opts;

  if (arguments.length >= 3) {
    view = arguments[1];
    opts = arguments[2];
  } else if (arguments.length >= 2) {
    opts = arguments[1];
  }

  opts = globalUtils.emptyObjectIfNullOrUndefined(opts);
  var backdropParent = opts.backdropParent;
  var contextData = opts.contextData;
  var lockViewConfirmLeave = (opts.lockViewConfirmLeave === true);
  var resetContent = (opts.resetContent === true);
  var resetData = !(opts.resetData === false);
  var initedFlag = jqElement.attr(CUSTOM_ATTR_INITED_FLAG);

  if (!(initedFlag === 'true')) {
    jqElement.on(_event.MODAL_SHOW, function() {
      jqElement.trigger(CUSTOM_EVENT_ON_SHOW);
    });

    jqElement.on(_event.MODAL_HIDE, function() {
      jqElement.trigger(CUSTOM_EVENT_ON_HIDE);
    });

    jqElement.on(_event.MODAL_HIDDEN, function () {
      // 解绑指定的事件
      jqElement.off(CUSTOM_EVENT_ON_SHOW);
      jqElement.off(CUSTOM_EVENT_ON_HIDE);
    });

    // 拷贝对话框的内容
    var contentHtml = jqElement.html();
    contentHtml = globalUtils.emptyStringIfNullOrUndefined(contentHtml);
    jqElement.data(JQUERY_DATA_CONTENT_BACKUP, contentHtml);

    // 标记对话框已被初始
    jqElement.attr(CUSTOM_ATTR_INITED_FLAG, 'true');
  } else {
      // 重置对话框的内容
      if (resetContent) {
        var contentHtml = jqElement.data(JQUERY_DATA_CONTENT_BACKUP);
        jqElement.html(contentHtml);
      }
  }

  // 锁定视图在离开时需要确认
  if (lockViewConfirmLeave) {
    if (globalUtils.isNullOrUndefined(view)) {
      throw new Error('opts.lockViewConfirmLeave true, but argument "view" missing')
    }

    var viewScope = view.getViewScope();
    _view.lockViewConfirmLeave(viewScope);
  }

  // 弹出对话框的默认参数
  var defaultOpts = {
    keyboard: false,
    backdrop: 'static'
  };
  // 创建事件处理函数的参数
  var eventData = {
    jqElement: jqElement,
    view: view,
    opts: opts,
    backdrop: 'static'
  };

  if (!resetContent && resetData) {
    // 记录对话框的初始数据
    var dialogDataModel = uiData.model(jqElement[0]);
    eventData.dialogDataModel = dialogDataModel;
    eventData.dialogInitialData = dialogDataModel.getData('*');
  }

  if (!globalUtils.isNullOrUndefined(backdropParent)) {
    defaultOpts.backdrop = false;
    eventData.backdrop = false;

    var jqBackdropParent = (backdropParent instanceof jQuery)
      ? backdropParent : jQuery(backdropParent);
    var jqBackdrop;

    if (jqBackdropParent.children('.modal-backdrop').length > 0) {
      jqBackdrop = jQuery('<div class="modal-backdrop" style="visibility:hidden;"></div>');
    } else {
      jqBackdrop = jQuery('<div class="modal-backdrop show"></div>');
    }

    eventData.jqBackdrop = jqBackdrop;
    eventData.jqBackdropParent = jqBackdropParent;
  }

  var showEventFn = createDialogShowEventFn(eventData);
  var hideEventFn = createDialogHideEventFn(eventData);
  // 绑定事件
  jqElement.on(CUSTOM_EVENT_ON_SHOW, showEventFn);
  jqElement.on(CUSTOM_EVENT_ON_HIDE, hideEventFn);
  // 设置对话框的上下文数据
  _dialog.setContextData(jqElement, contextData);

  var newOpts = globalUtils.mergeObjects(defaultOpts, opts);
  jqElement.modal(newOpts);

  return jqElement;
};

/**
 * @memberof uiUtils-dialog
 * @description 隐藏模态框
 * @param {jQuery} jqElement 
 * @returns {jQuery}
 */
_dialog.hideModal = function (jqElement) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  jqElement.modal('hide');

  return jqElement;
};


/**
 * @memberof uiUtils-dialog
 * @description 返回上下文数据
 * @param {jQuery} jqElement 
 * @returns {*}
 */
_dialog.getContextData = function (jqElement) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  return jqElement.data(JQUERY_DATA_CONTEXT_DATA);;
}

/**
 * @memberof uiUtils-dialog
 * @description 设置上下文数据
 * @param {jQuery} jqElement 
 * @param {*} data
 */
_dialog.setContextData = function (jqElement, data) {
  if (globalUtils.isNullOrUndefined(jqElement)) {
    throw new Error('argument#0 "jqElement" is null/undefined');
  }

  jqElement.data(JQUERY_DATA_CONTEXT_DATA, data);
}

/**
 * @memberof uiUtils-dialog
 * @description 创建对话框显示事件的处理函数
 * @param {*} eventData
 */
function createDialogShowEventFn(eventData) {
  if (globalUtils.isNullOrUndefined(eventData)) {
    throw new Error('argument#0 "eventData" is null/undefined');
  }

  var opts = eventData.opts;
  var jqBackdrop = eventData.jqBackdrop;
  var jqBackdropParent = eventData.jqBackdropParent;
  var onModalShow = opts.onModalShow;

  var eventFn = function (event) {
    if (!globalUtils.isNullOrUndefined(jqBackdropParent)) {
      jqBackdrop.prependTo(jqBackdropParent);
      jqBackdropParent.addClass('modal-open');
  
      if (globalUtils.isFunction(onModalShow)) {
        onModalShow();
      }
    }
  };

  return eventFn;
}

/**
 * @memberof uiUtils-dialog
 * @description 创建对话框隐藏事件的处理函数
 * @param {*} eventData
 */
function createDialogHideEventFn(eventData) {
  if (globalUtils.isNullOrUndefined(eventData)) {
    throw new Error('argument#0 "eventData" is null/undefined');
  }

  var jqElement = eventData.jqElement;
  var view = eventData.view;
  var opts = eventData.opts;
  var backdrop = eventData.backdrop;
  var onModalHide = opts.onModalHide;
  var lockViewConfirmLeave = (opts.lockViewConfirmLeave === true);
  var resetContent = (opts.resetContent === true);
  var resetData = !(opts.resetData === false);

  var eventFn = function (event) {
    if (resetContent) {
      var contentHtml = jqElement.data(JQUERY_DATA_CONTENT_BACKUP);
      // 重置对话框的内容
      jqElement.html(contentHtml);
    } else if (resetData) {
      var dialogDataModel = eventData.dialogDataModel;
      var dialogInitialData = eventData.dialogInitialData;
      // 重置对话框的数据
      dialogDataModel.setData('*', dialogInitialData);
    }

    // 重置上下文数据
    _dialog.setContextData(jqElement, null);

    // 解锁视图在离开时不需要确认
    if (lockViewConfirmLeave) {
      var viewScope = view.getViewScope();
      _view.unlockViewAllowLeave(viewScope);
    }

    if (globalUtils.isFunction(onModalHide)) {
      onModalHide();
    }

    if (backdrop === false) {
      var jqBackdrop = eventData.jqBackdrop;
      var jqBackdropParent = eventData.jqBackdropParent;
      // 移除遮罩层
      jqBackdrop.remove();

      if (jqBackdropParent.children('.modal-backdrop').length <= 0) {
        jqBackdropParent.removeClass('modal-open');
      }
    }
  };

  return eventFn;
}




/**
 * @namespace uiUtils-view
 */

var _view = {};
uiUtils.view = _view;
var VIEW_SCOPE_LOCK_COUNT = 'uiUtils.lockCount';
var VIEW_SCOPE_FORM_DATA = 'uiUtils.formData';

/**
 * @memberof uiUtils-view
 * @description 锁定视图在离开时需要确认
 * @param {Object} scope 
 */
_view.lockViewConfirmLeave = function (scope) {
  if (globalUtils.isNullOrUndefined(scope)) {
    throw new Error('argument#0 "scope" is null/undefined');
  }

  var lockCount = scope[VIEW_SCOPE_LOCK_COUNT];

  if (globalUtils.isNullOrUndefined(lockCount)) {
    lockCount = 1;
  } else {
    if (lockCount >= 1) {
      lockCount = lockCount + 1;
    } else {
      lockCount = 1;
    }
  }

  if (lockCount === 1) {
    scope.onViewClosing = function (view, nextFn) {
      uiUtils.dialog.showConfirmBox('${t("ui.system.common.msg.promptWillLeaveViewWhenLeave")}', nextFn);
    };
  }

  scope[VIEW_SCOPE_LOCK_COUNT] = lockCount;
};

/**
 * @memberof uiUtils-view
 * @description 解锁视图在离开时不需要确认
 * @param {Object} scope 
 */
_view.unlockViewAllowLeave = function (scope) {
  if (globalUtils.isNullOrUndefined(scope)) {
    throw new Error('argument#0 "scope" is null/undefined');
  }

  var lockCount = scope[VIEW_SCOPE_LOCK_COUNT];

  if (globalUtils.isNullOrUndefined(lockCount)) {
    lockCount = 0;
  } else {
    if (lockCount >= 1) {
      lockCount = lockCount - 1;
    } else {
      lockCount = 0;
    }
  }

  if (lockCount === 0) {
    scope.onViewClosing = undefined;
  }

  scope[VIEW_SCOPE_LOCK_COUNT] = lockCount;
};

/**
 * @memberof uiUtils-view
 * @description 离开视图时检查表单数据是否未保存
 * @param {Object} scope 
 * @param {function} dataFn
 */
_view.checkFormDataChangeWhenLeave = function (scope, dataFn) {
  if (globalUtils.isNullOrUndefined(scope)) {
    throw new Error('argument#0 "scope" is null/undefined');
  }

  if (globalUtils.isNullOrUndefined(dataFn)) {
    throw new Error('argument#1 "dataFn" is null/undefined');
  }

  // 记录当前表单数据到上下文中
  _view.setCurrentFormDataToContext(scope, dataFn);

  scope.onViewClosing = function (view, nextFn) {
    var newFormData = dataFn();
    var oldFormData = scope[VIEW_SCOPE_FORM_DATA];

    if (globalUtils.isFormDataEqual(oldFormData, newFormData)) {
      nextFn();
    } else {
      uiUtils.dialog.showConfirmBox('${t("ui.system.common.msg.promptDataNotSavedWhenLeave")}', nextFn);
    }
  };
};

/**
 * @memberof uiUtils-view
 * @description 离开视图时不检查表单数据是否未保存
 * @param {Object} scope 
 */
_view.notCheckFormDataChangeWhenLeave = function (scope) {
  if (globalUtils.isNullOrUndefined(scope)) {
    throw new Error('argument#0 "scope" is null/undefined');
  }

  scope.onViewClosing = undefined;
};


/**
 * @memberof uiUtils-view
 * @description 记录当前表单数据到上下文中
 * @param {Object} scope 
 * @param {(function|*)} data
 */
_view.setCurrentFormDataToContext = function (scope, data) {
  if (globalUtils.isNullOrUndefined(scope)) {
    throw new Error('argument#0 "scope" is null/undefined');
  }

  if (globalUtils.isNullOrUndefined(data)) {
    throw new Error('argument#1 "data" is null/undefined');
  }

  scope[VIEW_SCOPE_FORM_DATA] = globalUtils.isFunction(data)
    ? data() : data;
};



return uiUtils;
})();

/* SOURCE-CODE-END */

export { uiUtils };
